import { Message } from "element-ui";
/**
 * @description: 传入一个Dome用于分隔各个元素节点功能
 * @param {Array} childNodes 传入所有的插入的Dome元素
 * @return {object} 返回分割好的数组
 */
export function domeClassify(childNodes) {
    let dome = {
        text: "",
        screen: [],
        jpg: []
    }
    childNodes.forEach((item) => {
        //文本保存
        if (item.nodeType === 3) {
            dome.text += item.data
        } else {
            if (item.tagName === "SPAN") {     //保存全屏功能
                dome.screen.push(item.outerHTML)
            } else if (item.tagName === "I") {  //保存贴图功能
                dome.jpg.push(item.outerHTML)
            }
        }
    })
    return dome
}

/**
 * @description: 判断是否可以保存
 * @param {*} text 传入判断文字
 * @param {*} dome 传入screen全屏节点
 * @return {Boolean}
 */
export function screenOk(text, dome) {
    if (text.length > 300) {
        Message.error("输入的单页文字大于300");
        return false
    } else if (text.length < 10) {
        Message.error("输入的单页文字小于10");
        return false
    } else if (dome.length > 2) {
        Message.error("输入的@全屏@恢复指令过多哦");
        return false
    } else if (dome.length === 0) {
        return true
    } else {
        if (dome[0] !== '<span contenteditable="false" style="display: inline-block; font-size: 14px; border-radius: 4px; color: rgb(5, 144, 223); background-color: rgba(5, 144, 223, 0.098); width: 50px; line-height: 20px; height: 20px; text-align: center;">@全屏</span>') {
            Message.error("@全屏位置错误");
            return false
        } else if (dome[1] !== '<span contenteditable="false" style="display: inline-block; font-size: 14px; border-radius: 4px; color: rgb(5, 144, 223); background-color: rgba(5, 144, 223, 0.098); width: 50px; line-height: 20px; height: 20px; text-align: center;">@恢复</span>') {
            Message.error("@恢复位置错误");
            return false
        } else {
            return true
        }
    }
}

/**
 * @description: 除了dome排除其它元素
 * @param {*} dome 文字数组
 * @param {*} gifdome 需要排除的数组
 * @return {*} 返回处理好的chidList
 */
function domeScreen(dome, gifdome) {
    dome.forEach((item, index) => {
        if (gifdome.length > 0) {//筛选掉贴图
            gifdome.forEach(gif => {
                dome[index] = item.replaceAll(gif, "")
            })
        }
    })
    return dome
}

/**
 * @description: 处理全屏
 * @param {*} pageindex 单个page
 * @return {*} 处理好的数组
 */
export function pageIndexs(pageindex) {
    let childList = []
    if (pageindex.dome.length === 2) {
        let dome //处理好的三组
        pageindex.textdome = pageindex.textdome.replaceAll("<br>", "")
        let dome1 = pageindex.textdome.split(pageindex.dome[0])
        let dome2End3 = dome1[1].split(pageindex.dome[1])
        dome = [...[dome1[0]], ...dome2End3]
        dome = domeScreen(dome, pageindex.gifdome)
        dome.forEach((item, index) => {
            if (item) {
                if (index === 1) { //默认第二个全屏
                    childList.push({
                        ttsText: item,//文字
                        tts: "",//语音
                        mouthVideo: "",//人嘴视频
                        isScale: 1,//是否放大
                        index
                    })
                } else {
                    childList.push({
                        ttsText: item,//文字
                        tts: "",//语音
                        mouthVideo: "",//人嘴视频
                        isScale: 2,//是否放大
                        index
                    })
                }
            }
        })
    }
    //给他添加下标
    childList.forEach((item, index) => {
        item.index = index
    })
    return childList
}

/**
 * @description: 筛选掉除了jpg外的其它元素
 * @param {*} textdome 总字符串
 * @param {*} jpgDome jpg数组dome
 * @param {*} dome //全屏dome
 * @return {*} 处理好的jpgdome
 */
export function jpgScreen(textdome, jpgDome, dome) {
    if (jpgDome.length > 0) {
        dome.forEach(item => {
            textdome = textdome.replaceAll(item, "")
        })
        return jpgArray(textdome, jpgDome)
    }
}

/**
 * @description: 处理刷新之后会重置图片的问题
 * @param {*} futureChildList 以后的ChildList
 * @param {*} nowChildList 现在的childListC
 * @return {*} 处理合并后的childList
 */
export function disposeNow(futureChildList, nowChildList) {
    //vue缺陷必须要先push进去才会监听
    let difference
    try {
        difference = futureChildList.length - nowChildList.length

    } catch (err) {
        difference = 0
    }
    if (difference > 0) {
        for (let index = 0; index < difference; index++) {
            nowChildList.push({
                position: 0,
                width: 100,
                height: 100,
                x: 0,
                y: 0,
                textureId: 0,
                path: ''
            })
        }
    }
    //多删少补
    nowChildList.length = futureChildList.length
    futureChildList.forEach((element, index) => {
        // if (nowChildList[index] === undefined) {
        //     nowChildList[index] = element
        // }
        nowChildList[index].position = element.position
        nowChildList[index].textureId = element.textureId
    })
    return nowChildList
}

/**
 * @description: 
 * @param {*} textdome 赛选后的总字符串
 * @param {*} jpgDome jpg数组dome
 * @return {*} jpgdome处理好的元素
 */
function jpgArray(textdome, jpgDome) {
    let textureList = []
    jpgDome.forEach((item, index) => {
        textureList.push({
            position: 0,
            width: 100,
            height: 100,
            x: 0,
            y: 0,
            textureId: 0,
            path: ''
        })
        if (index === 0) { //第一个不需要排他法
            textureList[index].position = textdome.split(item)[0].length
        } else {
            textdome = textdome.replace(jpgDome[index - 1], "")
            textureList[index].position = textdome.split(item)[0].length
        }
        textureList[index].textureId = item.substr(item.indexOf("@") + 1, 4) - 0
    })
    return textureList
}
